package com.gxa.customer.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.pojo.MerchantSea;
import com.gxa.customer.mapper.MerchantMapper;
import com.gxa.customer.mapper.ProvinceMapper;
import com.gxa.customer.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, MerchantSea> implements MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;

    //随机生成不重复的5位数
    public static String getRandom(int len) {
        int rs = (int) ((Math.random() * 9 + 1) * Math.pow(10, len - 1));
        return String.valueOf(rs);
    }

    //新建商海信息
    @Override
    public R addMerchant (MerchantSea merchantSea){
        R r = new R();

        if (null == merchantSea) {
            r.setCode(500);
            r.setMsg("参数为空！");
            return r;
        }

        String s=getRandom(5);
        merchantSea.setInformationNumber(s);

        int result = merchantMapper.insert(merchantSea);
        if (result == 1) {
            r.setCode(200);
            r.setMsg("添加成功！");
            return r;
        } else {
            r.setCode(500);
            r.setMsg("添加失败！");
            return r;
        }
    }

    //删除具体商海
    @Override
    public R delMerchant(Long merchantSeaTableId) {
        R r = new R();
        int result = merchantMapper.deleteById(merchantSeaTableId);
        if(result > 0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }else {
            r.setCode(500);
            r.setMsg("删除失败！");
        }
        return r;
    }

    //修改商海上下架状态
    @Override
    public R updateStatus(MerchantSea merchantSea) {
        UpdateWrapper<MerchantSea> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("merchant_sea_table_id",merchantSea.getMerchantSeaTableId());
        updateWrapper.set("status",merchantSea.getStatus());
        int update = baseMapper.update(merchantSea,updateWrapper);
        R r = new R();
        if (update==1){
            r.setCode(200);
            r.setMsg("修改状态成功");
        }else {
            r.setCode(500);
            r.setMsg("修改状态失败");
        }
        return r;
    }

    //编辑具体商海信息
    @Override
    public R updateMerchant(MerchantSea merchantSea) {
        int i = merchantMapper.updateById(merchantSea);
        R r = new R();
        if (i==1){
            r.setCode(200);
            r.setMsg("更新成功");
        }else {
            r.setCode(500);
            r.setMsg("更新失败");
        }
        return r;
    }

    //查询所有商海信息（分页）
    @Cacheable(value = "merchant",key = "'selectAllMerchant'")
    @Override
    public JsonResult selectAllMerchant(Integer current, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer start = (current-1)*limit;
        Integer size = limit;

        List<Map<String, Object>> merchantSeas = merchantMapper.selectAllMercahnt(start, size);
        int i = merchantMapper.selectAllMercahntCount();
        if(i != 0){
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功！");
            jsonResult.setCount(i);
            jsonResult.setData(merchantSeas);
        }else {
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败！");
        }
        return jsonResult;
    }

    //查询具体商海详情
    @Override
    public R selectMerchantById(Long merchantSeaTableId) {
        R r = new R();
        Map<String, Object> map = merchantMapper.selectMerchantById(merchantSeaTableId);
        if(map != null){
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(map);
        }else {
            r.setCode(500);
            r.setMsg("查询失败！");
        }
        return r;
    }

    //查询国家对应的商海信息和国家对应的省份信息（分页）
    @Override
    public JsonResult selectMerchantByCountryId(Long countryId, Integer current, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer start = (current-1)*limit;
        Integer size = limit;

        List<Map<String, Object>> maps = merchantMapper.selectAllMercahnt(start, size);
        List<Map<String, Object>> maps1 = merchantMapper.selectProvinceByCountryId(countryId);
        int i = merchantMapper.selectMerchantByCountryIdCount(countryId);
        Map map = new HashMap();
        map.put("merchant",maps);
        map.put("province",maps1);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功！");
        jsonResult.setData(map);
        jsonResult.setCount(i);
        return jsonResult;
    }

    //根据国家和地区查询商海信息（分页）
    @Override
    public JsonResult selectMerchantByCountryIdAndProvinceId(Long countryId, Long provinceId, Integer current, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer start = (current-1)*limit;
        Integer size = limit;

        List<Map<String, Object>> maps = merchantMapper.selectMerchantByCountryIdAndProvinceId(countryId, provinceId, start, size);
        int i = merchantMapper.selectMerchantByCountryIdAndProvinceIdCount(countryId,provinceId);
        if(i != 0){
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功！");
            jsonResult.setCount(i);
            jsonResult.setData(maps);
        }else {
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败！");
        }
        return jsonResult;
    }

    //根据行业类型查询商海信息(分页)
    @Override
    public JsonResult selectMerchantByIndustryId(Long industryId, Integer current, Integer limit) {
        JsonResult jsonResult = new JsonResult();
        Integer start = (current-1)*limit;
        Integer size = limit;

        List<Map<String, Object>> maps = merchantMapper.selectMerchantByIndustryId(industryId, start, size);
        int i = merchantMapper.selectMerchantByIndustryIdCount(industryId);
        if(i != 0){
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功！");
            jsonResult.setCount(i);
            jsonResult.setData(maps);
        }else {
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败！");
        }
        return jsonResult;
    }

    //根据商海id查询商海对应的所有申请用户(分页)
    @Override
    public JsonResult selectAllUserByMerchantId(Long merchantSeaTableId, Integer current, Integer limit) {
        JsonResult jsonResult = new JsonResult();
        Integer start = (current-1)*limit;
        Integer size = limit;

        List<Map<String, Object>> maps = merchantMapper.selectAllUserByMerchantId(merchantSeaTableId, start, size);
        int i = merchantMapper.selectAllUserByMerchantIdCount(merchantSeaTableId);
        if(i != 0){
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功！");
            jsonResult.setCount(i);
            jsonResult.setData(maps);
        }else {
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败！");
        }
        return jsonResult;
    }

    //查询所有商海信息（按照浏览量倒序排序-app）
    @Override
    public R selectAllMerchantByPageView() {
        R r = new R();
        List<Map<String, Object>> maps = merchantMapper.selectAllMerchantByPageView();
        if(maps == null){
            r.setCode(500);
            r.setMsg("找不到参数！！！");
            return r;
        }else {
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(maps);
            return r;
        }
    }

    //app-查询具体商海详情（添加一次浏览量）
    @Override
    public R selectMerchantInformation(Long id, Long merchantSeaTableId) {
        R r = new R();
        Map<String, Object> map = merchantMapper.selectMerchantInformation(id, merchantSeaTableId);
        int i = merchantMapper.addPageView(merchantSeaTableId);
        if(map == null){
            r.setCode(500);
            r.setMsg("找不到参数！！！");
            return r;
        }else {
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(map);
            return r;
        }
    }

    //app-申请新增信息
    @Override
    public R addApplyInformation(Long id, Long merchantSeaTableId, String appUserTrueName, String phone, String notes) {
        R r = new R();
        //1、根据用户id和商海id查询是否在中间表中存在
        int i = merchantMapper.selectByUserIdAndMerchantId(id, merchantSeaTableId);
        if (i > 0) {
            //2、若存在，添加数据，修改申请状态
            //2.1 添加数据
            int a = merchantMapper.addApplyInformation(id, appUserTrueName, phone, notes);
            //2.2 修改申请状态和存入当前时间
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
            String format = dateFormat.format(date);
            int b = merchantMapper.updateApplyStatus(id, merchantSeaTableId,format);
            if(a==1&&b==1){
                r.setCode(200);
                r.setMsg("申请成功！");
            }else {
                r.setCode(500);
                r.setMsg("申请失败！");
            }
        } else {//3、若不存在
            //3.1 用户表添加数据
            int c = merchantMapper.addApplyInformation(id, appUserTrueName, phone, notes);
            //3.2 中间表添加id数据
            int d = merchantMapper.addIdInformation(id, merchantSeaTableId);
            // 3.3 更新收藏状态,添加申请时间
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
            String format = dateFormat.format(date);
            int e = merchantMapper.updateApplyStatus(id, merchantSeaTableId,format);
            if(c==1&&d==1&&e==1){
                r.setCode(200);
                r.setMsg("申请成功！");
            }else {
                r.setCode(500);
                r.setMsg("申请失败！");
            }
        }
        return r;
    }

    //app-收藏状态修改
    @Override
    public R updateCollectStatus(Long id, Long merchantSeaTableId,Integer collectStatus) {
        R r = new R();

        //1、查询中间表中是否存在
        int i = merchantMapper.selectByUserIdAndMerchantId(id, merchantSeaTableId);

        if(i > 0){
            //2、若存在，修改状态
            int i1 = merchantMapper.updateCollectStatus(id, merchantSeaTableId, collectStatus);
            if(i1>0 && collectStatus==1){
                r.setCode(200);
                r.setMsg("收藏成功！");
            }else if(i1>0 && collectStatus==0){
                r.setCode(200);
                r.setMsg("已取消收藏！！");
            }else {
                r.setCode(500);
                r.setMsg("操作失败！");
            }
        }else {
            //3、若不存在，添加id数据，并修改收藏状态
            int i1 = merchantMapper.addIdInformation(id, merchantSeaTableId);
            //4、修改收藏状态
            int i2 = merchantMapper.updateCollectStatus(id, merchantSeaTableId, collectStatus);
            if(i1>0&&i2>0){
                r.setCode(200);
                r.setMsg("收藏成功！");
            }else {
                r.setCode(500);
                r.setMsg("收藏失败！");
            }
        }
        return r;
    }

    //app-查询国家对应的商海信息和国家对应的省份
    @Override
    public R selectAllMerchantAPP(Long countryId) {
        R r = new R();
        List<Map<String, Object>> maps = merchantMapper.selectAllMerchantAPP(countryId);
        List<Map<String, Object>> maps1 = merchantMapper.selectProvinceByCountryId(countryId);
        Map map = new HashMap();
        map.put("merchant",maps);
        map.put("province",maps1);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(map);
        return r;
    }

    //app-查询国家和省份对应的商海信息
    @Override
    public R selectMerchantByCountryIdAndProvinceIdAPP(Long countryId, Long provinceId) {
        R r =new R();
        List<Map<String, Object>> maps = merchantMapper.selectMerchantByCountryIdAndProvinceIdAPP(countryId, provinceId);
        if(maps == null){
            r.setCode(500);
            r.setMsg("找不到参数！！！");
            return r;
        }else {
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(maps);
            return r;
        }
    }
}

